﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using Capacitacion.Models;

namespace Capacitacion.Controllers
{
    public class ListController : Controller
    {
        //
        // GET: /List/

        private readonly RestaurantsContext _context = new RestaurantsContext();

        public ActionResult Index(string genero)
        {
            ViewBag.Generos = _context.People.Select(r => r.Genero).Distinct();

            if (genero == "")
            {
                var model =
                    _context.People
                    .OrderByDescending(r => r.Apellido);

                return View(model);
            }
            else
            {
                var model =
                       _context.People
                       .OrderByDescending(r => r.Apellido)
                       .Where(r => r.Genero == genero || (genero == null));

                return View(model);
            }
        }

        public ActionResult Muestra()
        {

            return View(_context.People.ToList());
        }

        //
        // GET: /List/Details/5

        public ActionResult Details(int id)
        {
            Person person = _context.People.Find(id);
            return View(person);
        }

        //
        // GET: /List/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /List/Create

        [HttpPost]
        public ActionResult Create(Person person)
        {
            if (ModelState.IsValid)
            {

                person.Genero = person.Genero.ToUpper();
                _context.People.Add(person);
                _context.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(person);
        }

        //
        // GET: /Person/Edit/5

        public ActionResult Edit(int id)
        {
            Person person = _context.People.Find(id);
            return View(person);
        }

        //
        // POST: /Person/Edit/5

        [HttpPost]
        public ActionResult Edit(Person person)
        {
            if (ModelState.IsValid)
            {
                _context.Entry(person).State = EntityState.Modified;
                _context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(person);
        }

        //
        // GET: /Person/Delete/5

        public ActionResult Delete(int id)
        {
            Person person = _context.People.Find(id);
            return View(person);
        }

        //
        // POST: /Person/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Person person = _context.People.Find(id);
            _context.People.Remove(person);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _context.Dispose();
            base.Dispose(disposing);
        }
    }
}
